package com.example.activitytest

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.first_layout.*

class FirstActivity : AppCompatActivity() {
    //--------初始化题目-----
    //问题描述
    var quesiton= arrayOf("学霸题，数正方体，图中有几个正方体？","图中的国家是哪个？","图中的钟表显示几点？","图中是什么动物？")
    //问题图片
    var img = arrayOf(R.drawable.r_c,R.drawable.acc,R.drawable.clock,R.drawable.niu)
    //问题选项
    var anwser = arrayOf(
        arrayOf("A.12","B.11","C.10","D.9"),
        arrayOf("A.中国","B.美国","C.日本","D.韩国"),
        arrayOf("A.9点","B.12点","C.6点","D.8点"),
        arrayOf("A.狗","B.羊","C.牛","D.猫")
    )
    //问题答案
    var tof = arrayOf(
        arrayOf(0,0,1,0),
        arrayOf(1,0,0,0),
        arrayOf(0,0,0,1),
        arrayOf(0,0,1,0)
    )
    //当前页码
    var page= 0
    //提前查看答案与否
    var flag=arrayOf(0,0,0,0)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化题目首页
        setContentView(R.layout.first_layout)
        textView3.setText(quesiton[0])
        imageView.setImageResource(img[0])
        button1.setText(anwser[0][0])
        button2.setText(anwser[0][1])
        button3.setText(anwser[0][2])
        button4.setText(anwser[0][3])

        //两条提示语句
        var right="不要作弊！希望你对你的人生也是这个态度"
        var error="看了答案还选不对！重开吧"

        //四个选项按钮
        button1.setOnClickListener{
            if(flag[page]==1){
                val hint = if(tof[page][0]==1){right}else{error}
                Toast.makeText(this,hint,Toast.LENGTH_SHORT).show()
            }else{
                if (tof[page][0] == 1) {
                    val intent = Intent(this, RightActivity::class.java)
                    startActivity(intent)
                } else {
                    val intent = Intent(this, ErrorActivity::class.java)
                    startActivity(intent)
                }
            }
        }
        button2.setOnClickListener {
            if(flag[page]==1) {
                val hint = if(tof[page][1]==1){right}else{error}
                Toast.makeText(this,hint,Toast.LENGTH_SHORT).show()
            }else{
                if (tof[page][1] == 1) {
                    val intent = Intent(this, RightActivity::class.java)
                    startActivity(intent)
                } else {
                    val intent = Intent(this, ErrorActivity::class.java)
                    startActivity(intent)
                }
            }
        }
        button3.setOnClickListener {
            if(flag[page]==1) {
                val hint = if(tof[page][2]==1){right}else{error}
                Toast.makeText(this,hint,Toast.LENGTH_SHORT).show()
            }else{
                if (tof[page][2] == 1) {
                    val intent = Intent(this, RightActivity::class.java)
                    startActivity(intent)
                } else {
                    val intent = Intent(this, ErrorActivity::class.java)
                    startActivity(intent)
                }
            }
        }
        button4.setOnClickListener {
            if(flag[page]==1) {
                val hint = if (tof[page][3]==1) { right } else { error }
                Toast.makeText(this, hint, Toast.LENGTH_SHORT).show()
            }else{
                if (tof[page][3] == 1) {
                    val intent = Intent(this, RightActivity::class.java)
                    startActivity(intent)
                } else {
                    val intent = Intent(this, ErrorActivity::class.java)
                    startActivity(intent)
                }
            }
        }


        //下一题按钮
        button6.setOnClickListener {
            if(page ==quesiton.size-1){
                Toast.makeText(this,"这是最后一题啦",Toast.LENGTH_SHORT).show()
            }
            else {
                page++
                button1.setText(anwser[page][0])
                button2.setText(anwser[page][1])
                button3.setText(anwser[page][2])
                button4.setText(anwser[page][3])
                imageView.setImageResource(img[page])
                textView3.setText(quesiton[page])
            }
        }

        //上一题按钮
        button7.setOnClickListener {
            if(page ==0 ){
                Toast.makeText(this,"这已经是第一题啦",Toast.LENGTH_SHORT).show()
            }
            else{
                page--
                button1.setText(anwser[page][0])
                button2.setText(anwser[page][1])
                button3.setText(anwser[page][2])
                button4.setText(anwser[page][3])
                imageView.setImageResource(img[page])
                textView3.setText(quesiton[page])
            }
        }


        //答案按钮
        button5.setOnClickListener {
            val intent = Intent(this, AnswerActivity::class.java)
            var bundle = Bundle()
            for(i in 0..(quesiton.size-1)){
                if(tof[page][i]==1){
                    bundle.putInt("answer",i)
                    bundle.putInt("page",page)
                    intent.putExtras(bundle)
                    break
                }
            }
            startActivityForResult(intent, 1)
        }
    }
        //答案页面返回参数
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode){
            1 -> if (resultCode == RESULT_OK){
                var reflag :String = data?.getStringExtra("repage").toString()
                var tmp=reflag?.toInt()
                flag[tmp]=1
            }
        }
    }

//    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
//        menuInflater.inflate(R.menu.main,menu)
//        return true
//    }
//
//    override fun onOptionsItemSelected(item: MenuItem): Boolean {
//        when(item.itemId){
//            R.id.add_item -> Toast.makeText(this,"Your clicked Add",
//                                Toast.LENGTH_SHORT).show()
//            R.id.remove_item -> Toast.makeText(this,"Your clicked Remove",
//                                    Toast.LENGTH_SHORT).show()
//        }
//        return true
//    }
}